package models;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table;

import model.entityes.exception.ConstantValueException;

import play.db.ebean.Model;

@Entity
@Table(name = "constant_value")
public class ConstantValue extends Model
{
    private static final long serialVersionUID = 1L;

    @EmbeddedId
    protected ConstantValuePK id;
    @Column(columnDefinition="LONGTEXT")
    protected String value = "";

    public final static String KEY = "key_value";
    public final static String LANGUICH = "languich";
    public final static String VALUE = "value";

    public ConstantValue()
    {
	if( id == null ) id = new ConstantValuePK();
    }
    
    public static String getConstantValue(String key)
    {
	String standartValue = null;
	try
	{
	    List<ConstantValue> descConst = ConstantValue
		    .searchByKey(key);
	    if (descConst.size() == 1) standartValue = descConst.get(0)
		    .getValue();
	}
	catch (ConstantValueException e)
	{
	    e.printStackTrace();
	}

	return standartValue;
    }
    
    public static List<ConstantValue> searchByKey(String key) throws ConstantValueException
    {
	try
	{
	    return searchByKey(key, Langvich.RUSSIAN);
	}
	catch (ConstantValueException e)
	{
	    throw e;
	}
    }

    public static List<ConstantValue> searchByKey(String key, String lang)
	    throws ConstantValueException
    {
	try
	{
	    return ConstantValue.find.where().ilike(ConstantValue.KEY, key)
		    .ilike(ConstantValue.LANGUICH, lang).findList();
	}
	catch (Exception e)
	{
	    throw ConstantValueException.getExceptionWhitUserMessage("Ошибка: "
		    + e.getMessage());
	}
    }

    public static Model.Finder<Long, ConstantValue> find = new Model.Finder<Long, ConstantValue>(
	    Long.class, ConstantValue.class);

    public String getKey()
    {
        return id.key_value;
    }

    public void setId(String key)
    {
        this.id.key_value = key;
    }
    
    public String getLanguich()
    {
        return id.languich;
    }

    public void setLanguich(String lang)
    {
        this.id.languich = lang;
    }

    public String getValue()
    {
        return value;
    }

    public void setValue(String value)
    {
        this.value = value;
    }
}
